<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NetworkController
 *
 * @author mateusz.ledzewicz
 */
class Network_teachedController extends FormListController {
    protected function begin() {
        $this->view = new ViewNetwork_teached();
        
    }

    protected function listRun(){
        $this->view->setSpecialVar('list', Network_teached::getAll());
        $this->view->setModel(Network_teached::loadModel(), 'header'); 
    }
    
    protected function formRun(){
        $id = (isSet($_GET['id']) && !empty($_GET['id'])) ? $_GET['id'] : null;
        $id_parent = (isSet($_GET['parent']) && !empty($_GET['parent'])) ? $_GET['parent'] : null;
       
        $parent = Network_prepared::loadModel($id_parent, array('Network'));
        $struct = explode('-', $parent->id_network->structure );
        $number_in = $struct[0];
        $number_out= $struct[count($struct)-1];
        $trainingFiles = Training_data::getAll("where number_in = '{$number_in}' and number_out = '{$number_out}'");
        
        if(count($trainingFiles) == 0)
        {
            throw new Exception('Training data for structure('.$parent->id_network->structure.') of this network doesn\'t exist');
        }
        
     
        
        
        if(is_null($parent) || $parent ->NewRecord)
        {
            throw new Exception('Bad parent id');
        }
        
        $model = Network_teached::loadModel($id);
        
       if(count($_POST) > 0){
            $params = '';
            $model->setValues($_POST);
            if(is_null($model->name))
            {
                $model->name=$parent->name;
            }
          
            if($model->NewRecord && Network_teached::loadSpecialModel("name = '{$model->name}' ")->NewRecord == FALSE)
            {
                throw new Exception('Network with this name exists');
            }
             
            
            $model->id_network_prepared = $parent;
            $trainingFile = Training_data::loadModel($model->id_training_file);
            if(is_null($trainingFile) || $trainingFile->NewRecord)
            {
                throw new Exception('Training file id is incorrect');
            }
             
            $params .= BasicFunctions::prepareFileName($parent->name).' ';
            $params .= basename($trainingFile->file).' ';
            $params .= $model->rate.' ';
            $params .= $model->momentum.' ';
            $params .= $model->epoc.' ';
            $params .= $model->desired_error.' ';
            $params .= $model->bit_fail.' ';
            $params .= BasicFunctions::prepareFileName($model->name);
            
            copy($parent->file, basename($parent->file));
            copy($trainingFile->file, basename($trainingFile->file));
            exec(ConfigPath::network_teached.' '.$params, $output);
            
            $outputCount = count($output);
            
            if($output[$outputCount-2] != '1')
            {
                throw new Exception('NETWORK_TRAINING failed');
            }
            
            unlink(basename($parent->file));
            unlink(basename($trainingFile->file));
            $model->file=$output[$outputCount-1];
         
            rename($model->file, ConfigPath::network_teached_output_dir.$model->file);
            $model->file = ConfigPath::network_teached_output_dir.$model->file;
            $model->save();
            
            foreach($output as $key => $line){
                if($key >= $outputCount-2)
                    break;
                $cols = explode(':', $line);
                Data_network::saveNew($model->id_network_teached, Data_network::typeError, $cols[0], $cols[1], $cols[2]);
            }
            
            
            
            header("Location: index.php?step=network_teached");
       
       }
       $this->view->setModel($model, 'edit');
       $this->view->setSpecialVar('training_files', $trainingFiles);
    }


        
        
        //put your code here
}

?>
